<?php

/**
 * @classname 基础
 * @classaccess public
 * @decription 基础接口
 * @author 张旭刚
 * @updateTime 2018年05月26日
 * @errorCode 101000|获取数据失败
 * @errorCode 101001|参数错误
 * @errorCode 101002|参数错误
 * @errorCode 101003|没有找到数据
 * @errorCode 101004|参数错误
 * @errorCode 101005|参数错误
 * @errorCode 101006|获取失败
 * @errorCode 101007|获取验证码失败
 * @errorCode 101008|短信模板未找到
 * @errorCode 101009|短信接口未找到
 * @errorCode 101010|参数错误
 * @errorCode 101011|图片验证码输入错误
 */

namespace api\controllers;

use Yii;
use yii\web\Controller;
use api\models\RecommendPosition;
use api\models\RecommendPositionProduct;
use api\models\Special;
use api\models\Verify;

class SiteController extends PublicController
{
    /**
     * @see 欢迎接口
     * @describe 分销系统环境欢迎接口
     * @access public
     * @name welcome
     * @method POST 
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2018年05月26日,创建
     * 
     * @return welcome|string|欢迎辞
     */
    public function actionWelcome()
    {
        return $this->renderJson(['welcome' => Yii::t('app', 'Welcome to Fenxiao System!')]);
    }
    
    /**
     * @see 获取游客编号
     * @describe 获取游客编号
     * @access public
     * @name tourists
     * @method POST 
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2018年06月08日,创建
     * 
     * @return welcome|string|欢迎辞
     */
    public function actionTourists()
    {
        $guid = $this->getGuid();
        if(empty($guid)){return $this->renderJson(['code' => 0, 'msg' => '获取失败', 'data' => ['result' => false], 'errorCode' => 10106]);}
        return $this->renderJson(['code' => 1, 'msg' => '获取成功', 'data' => ['guid' => $guid], 'errorCode' => 0]);
    }
    
    /**
     * @see 页面内容
     * @describe 用了获取首页要显示的数据信息，本接口无效登录，位置从左侧开始，一条龙排列
     * @access public
     * @name index
     * @method POST 
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2018年05月31日,创建|张旭刚,2018年06月05日,逻辑设计与代码实现
     * 
     * @param pageTag|string|required|页面标识
     * 
     * @return welcome|string|返回数据待定
     */
    public function actionIndex()
    {
        $pageTag = Yii::$app->request->post('pageTag');
        if(empty($pageTag)){return $this->renderJson(['code' => 0, 'msg' => '参数错误', 'data' => ['result' => false], 'errorCode' => 101001]);}
        //$cacheKey = 'page_'.$pageTag;
        //$positions = $this->redisGet($cacheKey);
        //if(empty($positions)){
            $positions = RecommendPosition::find()->select('id,section,position,description')->where(['section' => $pageTag, 'status' => 1])->asArray()->all();
            foreach($positions as $key => $position){
                $data = RecommendPositionProduct::find()->where(['position_id' => $position['id'], 'status' => 1])->asArray()->all();
                foreach($data as $k => $v){
                    $data[$k]['image'] = 'http://static-fenxiao.ikang.com/'.$v['image'];
                }
                $positions[$key]['list'] = $data;
            }
            //$this->redisSet($cacheKey, $positions);
        //}
        if(empty($positions)){return $this->renderJson(['code' => 0, 'msg' => '获取数据失败', 'data' => ['result' => false], 'errorCode' => 101000]);}
        return $this->renderJson(['code' => 1, 'msg' => '获取数据成功', 'data' => $positions, 'errorCode' => 0]);
    }
    
    /**
     * @see 专题页
     * @describe 专题页接口,用来获取专题也要显示的内容，这个接口应用了缓存
     * @access public
     * @name subject
     * @method POST 
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2018年05月31日,创建
     * 
     * @param specialId|string|required|专题编号
     * 
     * @return welcome|string|返回数据待定
     */
    public function actionSubject()
    {
        $specialId = Yii::$app->request->post('specialId');
        if(empty($specialId)){return $this->renderJson(['code' => 0, 'msg' => '参数错误', 'data' => ['result' => false], 'errorCode' => 101002]);}
        //$cacheKey = 'special_'.$specialId;
        //$data = $this->redisGet($cacheKey);
        //if(empty($data)){
            $data = Special::getOneSpecial($specialId);
            //$this->redisSet($cacheKey, $data);
        //}
        if(empty($data)){return $this->renderJson(['code' => 0, 'msg' => '没有找到数据', 'data' => ['result' => false], 'errorCode' => 101003]);}
        return $this->renderJson(['code' => 1, 'msg' => '获取数据成功', 'data' => $data, 'errorCode' => 0]);
    }
    
    /**
     * @see 发送短信验证码
     * @describe 忘记密码的发送验证码
     * @access public
     * @name send-verify-code
     * @method POST 
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2018年06月01日,创建|张旭刚,2018年06月07日,完善验证码短信发送逻辑|张旭刚,2018年06月07日,增加验证码和header的guid参数
     * 
     * @header guid|string|required|客户端编号|用来标示客户端的唯一性，前端需要保持guid的唯一性
     * 
     * @param mobile|string|required|手机号码
     * @param imgVerify|string|required|图片验证码
     * @param sign|string|required|验证码
     * 
     * @return welcome|string|返回数据待定
     */
    public function actionSendVerifyCode()
    {
        $mobile = Yii::$app->request->post('mobile');
        if(empty($mobile)){return $this->renderJson(['code' => 0, 'msg' => '参数错误', 'data' => ['result' => false], 'errorCode' => 101004]);}
        $imgVerify = Yii::$app->request->post('imgVerify');
        if(empty($imgVerify)){return $this->renderJson(['code' => 0, 'msg' => '参数错误', 'data' => ['result' => false], 'errorCode' => 101010]);}
        if(!$this->checkImgVerify($imgVerify)){return $this->renderJson(['code' => 0, 'msg' => '图片验证码输入错误', 'data' => ['result' => false], 'errorCode' => 101011]);}
        $this->checkSign('imgVerify='.$imgVerify.'&mobile='.$mobile);
        $code = $this->getVerifyCode($mobile, 1);
        if(empty($code)){return $this->renderJson(['code' => 0, 'msg' => '获取验证码失败', 'data' => ['result' => false], 'errorCode' => 101007]);}
        $tmp = $this->config('verify_sms_template');
        if(empty($tmp)){return $this->renderJson(['code' => 0, 'msg' => '短信模板未找到', 'data' => ['result' => false], 'errorCode' => 101008]);}
        $content = str_replace('{code}', $code, $tmp);
        $api = $this->config('send_sms');
        if(empty($api)){return $this->renderJson(['code' => 0, 'msg' => '短信接口未找到', 'data' => ['result' => false], 'errorCode' => 101009]);}
        $result = $this->sendSms($mobile, $content);
        $verify = Verify::find()->where(['target' => $mobile, 'targetType' => 1])->one();
        $verify->send_time = time();
        if($result == 'success'){
            $verify->send_status = 1;
            $verify->save(false);
            return $this->renderJson(['code' => 1, 'msg' => '短信发送成功', 'data' => ['result' => true], 'errorCode' => 0]);
        }
        $verify->send_status = 2;
        $verify->save(false);
        return $this->renderJson(['code' => 1, 'msg' => '短信发送失败', 'data' => ['result' => true], 'errorCode' => 101019]);
    }
    
    /**
     * @see 图片验证码
     * @describe 用来生产图片验证码的接口
     * @access public
     * @name get-img-verify
     * @method POST 
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2018年06月11日,创建并完善逻辑
     * 
     * @header guid|string|required|客户端编号|用来标示客户端的唯一性，前端需要保持guid的唯一性
     * 
     * @param base64|boolean|unrequired|使用base64|使用base64方式显示图片，缺省值为：1,选项：0,1
     * 
     * @return imgstr|string|返回图片代码
     */
    public function actionGetImgVerify()
    {
        $guid = $this->headers['guid'];
        $base64 = Yii::$app->request->post('base64');
        if(empty($base64)){$base64 = false;}
        if($base64 > 0){$base64 = true;}
        $verify = Yii::$app->verifyImg;
        $code = $verify->doimg($base64);
        $c = $verify->getCode();
        $this->redis->set('session_'.$guid.'_verifyCode', $c);
        if($base64 > 0){
            return $this->renderJson(['code' => 1, 'msg' => '验证码获取成功', 'data' => ['img' => $code, 'code' => $c], 'errorCode' => 0]);
        }
    }
    
    /**
     * @see 发送邮箱验证码！
     * @describe 忘记密码 - 发送邮箱验证码（当且仅当是邮箱密码登录的老用户时有效，新用户无需找回密码）
     * @access public
     * @name send-mail-verify
     * @method POST 
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2018年06月11日,创建并完善逻辑
     * 
     * @header guid|string|required|客户端编号|用来标示客户端的唯一性，前端需要保持guid的唯一性
     * 
     * @param email|string|required|邮箱地址
     * @param imgVerify|string|required|图片验证码
     * @param sign|string|required|验证码
     * 
     * @return welcome|string|返回数据待定
     */
    public function actionSendMailVerify()
    {
        $email = Yii::$app->request->post('email');
        if(empty($email)){return $this->renderJson(['code' => 0, 'msg' => '参数错误', 'data' => ['result' => false], 'errorCode' => 101012]);}
        $imgVerify = Yii::$app->request->post('imgVerify');
        if(empty($imgVerify)){return $this->renderJson(['code' => 0, 'msg' => '参数错误', 'data' => ['result' => false], 'errorCode' => 101013]);}
        if(!$this->checkImgVerify($imgVerify)){return $this->renderJson(['code' => 0, 'msg' => '图片验证码输入错误', 'data' => ['result' => false], 'errorCode' => 101014]);}
        $this->checkSign('imgVerify='.$imgVerify.'&email='.$email);
        $code = $this->getVerifyCode($email, 2);
        if(empty($code)){return $this->renderJson(['code' => 0, 'msg' => '获取验证码失败', 'data' => ['result' => false], 'errorCode' => 101015]);}
        $tmp = $this->config('verify_email_template');
        if(empty($tmp)){return $this->renderJson(['code' => 0, 'msg' => '邮件模板未找到', 'data' => ['result' => false], 'errorCode' => 101016]);}
        $content = str_replace('{code}', $code, $tmp);
        $result = $this->sendEmail([$email], $this->config('verify_email_subject'), $content);
        $verify = Verify::find()->where(['target' => $email, 'targetType' => 2])->one();
        $verify->send_time = time();
        if($result == 'send_success'){
            $verify->send_status = 1;
            $verify->save(false);
            return $this->renderJson(['code' => 1, 'msg' => '邮件发送成功', 'data' => ['result' => true], 'errorCode' => 0]);
        }
        $verify->send_status = 2;
        $verify->save(false);
        return $this->renderJson(['code' => 1, 'msg' => '邮件发送失败', 'data' => ['result' => true], 'errorCode' => 101017]);
    }
    
    /**
     * @see 设置新密码！
     * @describe 忘记密码 - 设置新密码,当且仅当用户为老用户（PC端使用邮箱密码登录的状况下需要，新用户使用手机验证码登录的无需找回密码）
     * @access public
     * @name set-pwd
     * @method POST 
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2018年06月01日,创建
     * 
     * @param verify|string|required|短信验证码
     * @param pwd|string|required|新密码
     * @param repwd|string|required|确认新密码
     * @param sign|string|required|验证码
     * 
     * @return welcome|string|返回数据待定
     */
    public function actionSetPwd()
    {
        $verify = Yii::$app->request->post('verify');
        if(empty($verify)){return $this->renderJson(['code' => 0, 'msg' => '参数错误', 'data' => ['result' => false], 'errorCode' => 101012]);}
        $pwd = Yii::$app->request->post('pwd');
        if(empty($pwd)){return $this->renderJson(['code' => 0, 'msg' => '参数错误', 'data' => ['result' => false], 'errorCode' => 101013]);}
        $repwd = Yii::$app->request->post('repwd');
        if(empty($repwd)){return $this->renderJson(['code' => 0, 'msg' => '参数错误', 'data' => ['result' => false], 'errorCode' => 101014]);}
        $this->checkSign('pwd='.$pwd.'&repwd='.$repwd.'&verify='.$verify);
        
        
    }
    
    /**
     * @see 校验码获取
     * @describe 测试用的校验码获取方法，上线时请删除该方法
     * @access public
     * @name get-sign
     * @method POST 
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2018年06月11日,创建
     * 
     * @param data|string|required|校验码原文
     * 
     * @return sign|string|校验码数据
     */
    public function actionGetSign()
    {
        $data = Yii::$app->request->post('data');
        return $this->renderJson([$this->getSign($data)]);
    }
    
    /**
     * @see 客户登出！
     * @describe 客户登出，从客户端删除登录信息，同时服务端也删除相应信息
     * @access public
     * @name cart-goods-list
     * @method POST 
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2018年06月01日,创建
     * 
     * @return welcome|string|返回数据待定
     */
    public function actionExit()
    {
        return $this->render('exit');
    }
    
     /**
     * @see 客户邮箱登录！
     * @describe 客户通过邮箱密码登录产生令牌，并与客户关联，并邀请客户端保存令牌
     * @access public
     * @name mail-login
     * @method POST 
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2018年06月01日,创建
     * 
     * @return welcome|string|返回数据待定
     */
    public function actionMailLogin()
    {
        return $this->render('login');
    }

    /**
     * @see 客户登录！
     * @describe 服务端产生登录令牌，并与客户关联，并邀请客户端保存令牌
     * @access public
     * @name login
     * @method POST 
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2018年06月01日,创建
     * 
     * @return welcome|string|返回数据待定
     */
    public function actionLogin()
    {
        return $this->render('login');
    }
    
    /**
     * @see 微信登录！
     * @describe 调用微信登录，登录之后的操作需要产品设计
     * @access public
     * @name wx-login
     * @method POST 
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2018年06月01日,创建
     * 
     * @return welcome|string|返回数据待定
     */
    public function actionWxLogin()
    {
        return $this->render('wx-login');
    }

    /**
     * @see 淘宝登录！
     * @describe 调用淘宝登录，登录之后的操作需要产品设计
     * @access public
     * @name wx-login
     * @method POST 
     * @requestType FORM
     * @defget no
     * @author 张旭刚,2018年06月01日,创建
     * 
     * @return welcome|string|返回数据待定
     */
    public function actionTaobaoLogin()
    {
        return $this->render('wx-login');
    }
}
